﻿using System.Collections.Generic;
using Chloe.MySql;
using First.DB;
using First.Model;

namespace First.DAL
{
    public class StudentDAL
    {
        MySqlContext context = new MySqlContext(new MySqlConnectionFactory(""));
        /// <summary>
        /// 返回一条学生信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Student ResultSingleStudent(int id)
        {
            return context.Query<Student>().Where(s => s.id == id).FirstOrDefault();
        }
        /// <summary>
        /// 获取所有学生信息
        /// </summary>
        /// <returns></returns>
        public List<Student> GetStudents()
        {
            return context.Query<Student>().Where(s => s.id > 0).ToList();
        }
        /// <summary>
        /// 获取所有学生详细信息
        /// </summary>
        /// <returns></returns>
        public List<ViewStudentDetail> GetStudentDetails()
        {
            return context.Query<ViewStudentDetail>().Where(s => s.id > 0).ToList();
        }

        /// <summary>
        /// 删除一个学生
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelStudent(int id)
        {
            return (int)context.Delete<Student>(s => s.id == id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="student"></param>
        public void EditStudent(Student student)
        {
            context.Update<Student>(s => s.id == student.id, s => new Student() { Name = student.Name, Age = student.Age, Sex = student.Sex, Phone = student.Phone, Qq = student.Qq, Wechat = student.Wechat, Classid = student.Classid });
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="student"></param>
        public void AddStudent(Student student)
        {
            context.Insert<Student>(() => new Student() { Name = student.Name, Age = student.Age, Sex = student.Sex, Phone = student.Phone, Qq = student.Qq, Wechat = student.Wechat, Classid = student.Classid });
        }
    }
}
